package com.itheima.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @Author weizhongli
 * @Date 2020/5/5
 * @Version v1.0
 * @Description ES配置类
 **/
@Configuration
@Component
@ConfigurationProperties(prefix = "elasticsearch")
//将配置文件的值注入到spring对象中
/*
 * 前提：
 * 1.当前配置类必须用@Component注解修饰
 * 2.当前类的成员变量，必须有getter和setter
 * */
public class ElasticsearchConfiguration {


    private String hostname = "127.0.0.1";
    private int port = 9200;

    /*
     * 操作对象注入spring容器
     * */
    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() {
        //硬编码
        RestClientBuilder clientBuilder = RestClient.builder(new HttpHost(hostname, port, "http"));
        return new RestHighLevelClient(clientBuilder);
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}



















